package Euler44;

import java.util.*;
import ReusableCode.*;

public class PentagonNumbers {
	public static void main(String[] args) {
		long max = 10000000000L;
		List<Long> pent = GeneralFunctions.generatePentagonNumbers(max);
		HashSet<Long> pentHash = GeneralFunctions.generatePentagonNumbersHash(max);
		List<List<Long>> pairs = new ArrayList<List<Long>>();
		
		for(Iterator<Long> iter1 = pent.iterator(); iter1.hasNext();)
		{
			long num1 = iter1.next();
			
			for(Iterator<Long> iter2 = pent.iterator(); iter2.hasNext();)
			{
				long num2 = iter2.next();
				if(num2<num1){
					if(isValid(num2, num1, pentHash))
					{
						//pairs.add(Arrays.asList(num2, num1));
						System.out.println(num1-num2);
						return;
					}
				}
				else
				{
					break;
				}
			}
		}
		
		/*System.out.println(pent);
		for(Iterator<List<Long>> iter = pairs.iterator(); iter.hasNext();)
		{
			System.out.println(iter.next());
		}*/
	}
	
	static boolean isValid(long a, long b, HashSet<Long> pent)
	{
		if(!pent.contains(b-a))
		{
			return false;
		}
		else if(!pent.contains(b+a))
		{
			return false;
		}
		
		return true;
	}
}
